package com.think.oauth.user.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.oauth.user.dao.user.UserDao;
import com.think.oauth.user.dao.user.UserOauthDao;
import com.think.oauth.user.model.Role;
import com.think.oauth.user.model.User;
import com.think.oauth.user.model.UserOauth;
import com.think.oauth.user.model.UserRole;

@Service
public class UserService extends ServiceImpl<UserDao, User> {


	
	@Autowired
	private UserOauthService oauthService;
	
	@Autowired
	private UserRoleService userRoleService;
	
	@Autowired
	private RoleService roleService;
	
	@Transactional
	public void insertUser(User user,UserOauth oauth) {
		try {
			this.save(user);
			oauth.setUserId(user.getId());
			oauthService.save(oauth);
			List<Role> roles = roleService.list(new QueryWrapper<Role>().eq("is_default", 1));
			Collection<UserRole> userRoles = new ArrayList<UserRole>();
			for (Role role : roles) {
				UserRole userrole = new UserRole();
				userrole.setUserId(user.getId());
				userrole.setRoleId(role.getRoleId());
				userrole.setCreator(user.getCreator());
				userrole.setUpdator(user.getUpdator());
				userRoles.add(userrole);
			}
			userRoleService.saveBatch(userRoles);
			
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException("数据库异常");
		}
			
	}
	
	public User getValidUser(Integer id) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.eq("id", id).eq("state", 1).eq("delflag", 0);
		return this.getOne(queryWrapper);
	}
	
	@Transactional
	public void insertUser(User user,List<UserOauth>  oauths) {
		try {
			this.save(user);
			for (int i = 0; i < oauths.size(); i++) {
				oauths.get(i).setUserId(user.getId());
				oauthService.save(oauths.get(i));
			}
			List<Role> roles = roleService.list(new QueryWrapper<Role>().eq("is_default", 1));
			Collection<UserRole> userRoles = new ArrayList<UserRole>();
			for (Role role : roles) {
				UserRole userrole = new UserRole();
				userrole.setUserId(user.getId());
				userrole.setRoleId(role.getRoleId());
				userrole.setCreator(user.getCreator());
				userrole.setUpdator(user.getUpdator());
				userRoles.add(userrole);
			}
			userRoleService.saveBatch(userRoles);
		}
		catch(DuplicateKeyException ex) {
			throw new RuntimeException("用户名或者邮箱重复！");
		}
		catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			throw new RuntimeException("数据库异常");
		}
			
	}
	
	public User selectUserWithOauth(String identifier,Integer loginType) {
		return baseMapper.selectUser(identifier,loginType);
			
	}
	
	public Integer selectUserIdWithOauth(String identifier,Integer loginType) {
		return baseMapper.selectUserIdWithOauth(identifier,loginType);
	}
	@Transactional
	public boolean updateUser(User user,List<UserRole> roles) {
	
		try {
			baseMapper.updateUser(user);
			if (roles !=null) {
				QueryWrapper<UserRole>  queryWrapper  = new  QueryWrapper<>();
				queryWrapper.eq("user_id", user.getId());
				userRoleService.remove(queryWrapper);
			
				userRoleService.saveBatch(roles);
			}
			
		} catch (Exception e) {
			throw new RuntimeException("数据库异常");
		}
		return true;
	}
	
	
	
}
